<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Examples extends Model
{
    protected $table = 'examples';
    public $timestamps = false;
    protected $guarded = [
        'id', 'backgroundImage', 'description', 'view', 'area', 'create_at', 'updated_at'
    ];

    public function getData($search = [], $page = 1, $length = 20){
        $ExamplesList = array();
        $ExamplesList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table)
            ->whereNull('deleted_at');

        if(isset($search['title'])){
            $sql->where('title', 'like', '%'.$search['title'].'%');
        }
        if(isset($search['id'])){
            $sql->where('id', 'like', '%'.$search['id'].'%');
        }
        $ExamplesList['pageData']['count']     = $sql->count();
        $ExamplesList['pageData']['lastPage']  = ceil($ExamplesList['pageData']['count'] / $length);

        $ExamplesList['list'] = $sql->orderBy('view', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $ExamplesList;
    }
// 图片展示
    public function getDataimg($id){
        $ExamplesList = array();

        $sql = \DB::table('example_img')
            ->where('example_id',$id)
            ->whereNull('deleted_at');

        $ExamplesList['pageData']['count']     = $sql->count();

        $ExamplesList['list']=$sql->orderBy('created_at', 'desc')
                          ->get();

        return $ExamplesList;
    }
    //装修案例详情
    public function getDatalist($id=[]){
        $examplesList = array();
        $sql = \DB::table($this->table.' as p')
            ->select('m.img')
            ->leftjoin('example_img as m' , 'm.example_id' , '=' , 'p.user_id')
            ->whereNull('p.deleted_at')
            ->where('p.user_id',$id);

        $examplesList['list'] = $sql
            ->orderBy('p.created_at', 'desc')

            ->get();

        return $examplesList;
    }


    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }
//  保存装修图片
    public function saveImg($data){
        return \DB::table('example_img')->insert($data);
    }

    public function setData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }

    public function delData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }

    //删除图集
    public function delDataimg($id,$data){
        return \DB::table('example_img')->where('id',$id)->update($data);
    }
}